package com.banksoft.cps.task;

import com.banksoft.cps.bean.Device;
import com.banksoft.cps.bean.MonitorProduct;
import com.banksoft.cps.bean.MonitorValueLora;
import com.banksoft.cps.bean.SaleOrder;
import com.banksoft.cps.bean.warpper.LoraValueJjllWarpper;
import com.banksoft.cps.dao.*;
import com.banksoft.cps.util.StringNumber;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/9/14.
 */
@Service
public class LoraDataCalcTask {

    @Autowired
    LoraValueJjllDao loraValueJjllDao;
    @Autowired
    SaleOrderDao saleOrderDao;
    @Autowired
    DeviceDao deviceDao;
    @Autowired
    MonitorProductDao monitorProductDao;
    @Autowired
    MonitorValueLoraDao monitorValueLoraDao;

    public void execute(){
        long now  = System.currentTimeMillis();
        /**
         * 查询结束时间在10分钟之前的
         */
        List<SaleOrder> saleOrders =  saleOrderDao.needHandleSaleOrder(now-600000);
        if(!saleOrders.isEmpty()){
            saleOrders.stream().forEach(e->{
                try{
                    String deviceId =  e.getDeviceId();
                    if(StringUtils.isNotBlank(deviceId)){
                        Device device  = deviceDao.findById(deviceId);
                        MonitorProduct  monitorProduct = monitorProductDao.findById(device.getProductId());
                        if(device !=null && StringUtils.isNotBlank(device.getDeveui())){
                            LoraValueJjllWarpper  warpper = loraValueJjllDao.
                                    findByDeveui(device.getDeveui(),e.getTimeB(),e.getTimeE());
                            String value = StringNumber.sub(warpper.getMaxValue(),warpper.getMinValue());
                            MonitorValueLora bean = new MonitorValueLora();
                            bean.setId(UUIDFactory.random());
                            bean.setSaleOrderId(e.getId());
                            bean.setValue(value);
                            bean.setBeginValue(warpper.getMinValue());
                            bean.setEndValue(warpper.getMaxValue());
                            bean.setBeginTime(warpper.getMinTime());
                            bean.setEndTime(warpper.getMaxTime());
                            if(monitorProduct !=null){
                                bean.setModulus(monitorProduct.getModulus());
                            }
                            monitorValueLoraDao.insert(bean);
                            saleOrderDao.setCalcFlag(Arrays.asList(e.getId()));
                        }
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                }

            });
        }

    }

    private boolean isValid(long time1,long time2){
        boolean valid = false;

        return valid;
    }
}
